package com.android.wlw_local.service;

/**
 * Created by zhuqin on 2018/1/3.
 */



import android.app.NotificationManager;
import android.content.Intent;
import android.net.NetworkInfo;
import android.util.Log;

import com.farsunset.cim.sdk.android.CIMEventBroadcastReceiver;
import com.farsunset.cim.sdk.android.CIMListenerManager;
import com.farsunset.cim.sdk.android.model.Message;
import com.farsunset.cim.sdk.android.model.ReplyBody;

import org.greenrobot.eventbus.EventBus;

/**
 *  消息入口，所有消息都会经过这里
 * @author 3979434
 *
 */
public final class CIMPushManagerReceiver extends CIMEventBroadcastReceiver {

    private NotificationManager notificationManager;

    //当收到消息时，会执行onMessageReceived，这里是消息第一入口

    @Override
    public void onMessageReceived(Message message, Intent intent) {
        Log.i("zhuqin","onMessageReceived:"+message.toString());
        //调用分发消息监听
        CIMListenerManager.notifyOnMessageReceived(message);

        //以开头的为动作消息，无须显示,如被强行下线消息Constant.TYPE_999
        if(message.getAction().startsWith("9"))
        {
            return ;
        }

//        showNotify(context,message);
    }



//    private void  showNotify(Context context , Message msg)
//    {
//
//        this.notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//        String title  = "系统消息";
//
//        PendingIntent contentIntent = PendingIntent.getActivity(context,1, new Intent(context,SystemMessageActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
//        final NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
//        builder.setAutoCancel(true);
//        builder.setDefaults(Notification.DEFAULT_ALL);
//        builder.setWhen(msg.getTimestamp());
//        builder.setSmallIcon(R.drawable.icon);
//        builder.setTicker(title);
//        builder.setContentTitle(title);
//        builder.setContentText(msg.getContent());
//        builder.setDefaults(Notification.DEFAULT_LIGHTS);
//        builder.setContentIntent(contentIntent);
//        final Notification notification = builder.build();
//
//
//        notificationManager.notify(R.drawable.icon, notification);
//
//    }


    @Override
    public void onNetworkChanged(NetworkInfo info) {
        CIMListenerManager.notifyOnNetworkChanged(info);
    }


    @Override
    public void onConnectionSuccessed(boolean hasAutoBind) {
        CIMListenerManager.notifyOnConnectionSuccessed(hasAutoBind);
    }

    @Override
    public void onConnectionClosed() {
        CIMListenerManager.notifyOnConnectionClosed();
    }


    @Override
    public void onReplyReceived(ReplyBody body) {
        Log.i("zhuqin","onReplyReceived:"+body.toString());
        CIMListenerManager.notifyOnReplyReceived(body);
        EventBus.getDefault().post(body);
    }



    @Override
    public void onConnectionFailed() {
        // TODO Auto-generated method stub
        CIMListenerManager.notifyOnConnectionFailed();
    }

}
